package com.axend.aerosense.room.ui.fragment.vipfun;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.axend.aerosense.room.entity.RoomRemindBean;
import com.axend.aerosense.room.entity.RoomVipSetBean;
import com.axend.aerosense.room.ui.activity.RoomVipFunActivity;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.axend.aerosense.common.ui.BottomTextPickerDialog;
import com.axend.aerosense.room.BR;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentSedentaryAnalysisBinding;

import java.util.Map;

/**
 * 久坐分析
 * Author: ChenHongWei
 * Date: 2021/3/25 11:22
 */
public class RoomSedentaryAnalysisFragment extends BaseVipFunFragment<RoomFragmentSedentaryAnalysisBinding> {

    private Map<String, Float> timeMap;
    private int continuousSelectValue;  //连续久坐选择值
    private int accumulateSelectValue;  //累积久坐选择值
    private String[] timeList;

    public static RoomSedentaryAnalysisFragment getInstance(){
        return new RoomSedentaryAnalysisFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_sedentary_analysis;
    }

    @Override
    public int getBindingVariable() {
        return BR.viewModel;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onChange(CompoundButton buttonView, boolean isChecked) {
        super.onChange(buttonView, isChecked);
        viewModel.continuousSedentary.setValue(isChecked);
    }

    @Override
    protected void saveSettings() {
        super.saveSettings();
        RoomRemindBean continuitySitBean = new RoomRemindBean(
                getRoomUuid(),
                getRoomType(),
                getCurrentActivity().getSwitchState(),
                continuousSelectValue);

        RoomRemindBean accumulateSitBean = new RoomRemindBean(
                getRoomUuid(),
                getRoomType(),
                viewDataBinding.roomAccumulateSit.getSwitchChecked() ? 1 : 0,
                accumulateSelectValue);

        viewModel.saveSedentaryData(continuitySitBean, accumulateSitBean);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getCurrentActivity().setVipFunTip(R.string.room_sedentary_analysis_tips);
        getCurrentActivity().setLeftText(getString(R.string.room_sedentary_switch_text));
        setTitle(R.string.room_sedentary_analysis);

        init();
    }

    private void init(){
        timeMap = getTimeList(1f, 12f, 0.5f, getUnit());
        timeList = timeMap.keySet().toArray(new String[timeMap.size()]);

        //连续久坐
        viewDataBinding.roomAlarmConstraintLayout.setOnClickListener(v -> {
            BottomTextPickerDialog fragment = new BottomTextPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setWheelItemTextSize(16)
                    .setDefValue(4)  //默认值（数组下标）
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setIndexCallBack(new OnDateSetListener<Integer>() {
                        @Override
                        public void onDateSet(Fragment timePickerView, Integer sex) {
                            viewDataBinding.roomAlarmTime.setText(timeList[sex]);
                            continuousSelectValue = (int)(timeMap.get(timeList[sex]) * 60);
                            change();
                        }
                    })
                    .build(timeList);
            fragment.show(getChildFragmentManager(), BottomTextPickerDialog.class.getSimpleName());
        });
//        //警告设置
//        viewDataBinding.roomWarningConstraintLayout.setOnClickListener(v -> {
//            BottomNumberPickerDialog fragmentDialog = new BottomNumberPickerDialog.Builder()
//                    .setType(Type.NUMBER)
//                    .setMinValue(1)  //最小3
//                    .setMaxValue(24)  //最大
//                    .setDefValue(3)  //默认
//                    .setUnit(getUnit())  //单位
//                    .setWheelItemTextSize(16)
//                    .setStep(1)  //步长
//                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
//                    .setCallBack((timePickerView, value) -> {
//                        viewDataBinding.roomWarningTime.setText(value + getUnit());
//                    })
//                    .build();
//            fragmentDialog.show(getChildFragmentManager(), fragmentDialog.getClass().getSimpleName());
//        });

        //提醒设置
        viewDataBinding.roomAlarmConstraintLayout2.setOnClickListener(v -> {
            BottomTextPickerDialog fragment = new BottomTextPickerDialog.Builder()
                    .setType(Type.NUMBER)
                    .setWheelItemTextSize(16)
                    .setDefValue(10)  //默认值（数组下标）
                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
                    .setIndexCallBack(new OnDateSetListener<Integer>() {
                        @Override
                        public void onDateSet(Fragment timePickerView, Integer sex) {
                            viewDataBinding.roomAlarmTime2.setText(timeList[sex]);
                            accumulateSelectValue = (int)(timeMap.get(timeList[sex]) * 60);
                            change();
                        }
                    })
                    .build(timeList);
            fragment.show(getChildFragmentManager(), BottomTextPickerDialog.class.getSimpleName());
        });
//        //警告设置
//        viewDataBinding.roomWarningConstraintLayout2.setOnClickListener(v -> {
//            BottomNumberPickerDialog fragmentDialog = new BottomNumberPickerDialog.Builder()
//                    .setType(Type.NUMBER)
//                    .setMinValue(1)  //最小3
//                    .setMaxValue(24)  //最大
//                    .setDefValue(3)  //默认
//                    .setUnit(getUnit())  //单位
//                    .setWheelItemTextSize(16)
//                    .setStep(1)  //步长
//                    .setThemeColor(getResources().getColor(R.color.app_colorAccent))
//                    .setCallBack((timePickerView, value) -> {
//                        viewDataBinding.roomWarningTime2.setText(value + getUnit());
//                    })
//                    .build();
//            fragmentDialog.show(getChildFragmentManager(), fragmentDialog.getClass().getSimpleName());
//        });

        RoomVipSetBean vipSettings = getVipSettings();
        if (vipSettings != null){
            getCurrentActivity().setSwitchChange(vipSettings.getFunctionSwitch() == 1);
//            viewDataBinding.roomWarningTime.setText((vipSettings.getFunctionWarning() / 60) + getUnit());
            continuousSelectValue = vipSettings.getFunctionRemind();
            viewDataBinding.roomAlarmTime.setText((vipSettings.getFunctionRemind() / 60f)+" " + getUnit());
        }
        RoomVipSetBean accumulateSit = (RoomVipSetBean) getCurrentActivity()
                .getIntent()
                .getSerializableExtra(RoomVipFunActivity.FLAG_ACCUMULATE_SIT);
        if (accumulateSit != null){
            viewModel.cumulativeSedentary.setValue(accumulateSit.getFunctionSwitch() == 1);
            accumulateSelectValue = accumulateSit.getFunctionRemind();
//            viewDataBinding.roomWarningTime2.setText((accumulateSit.getFunctionWarning() / 60) + getUnit());
            viewDataBinding.roomAlarmTime2.setText((accumulateSit.getFunctionRemind() / 60f)+" " + getUnit());
        }
    }

    private String getUnit(){
        return " " + getString(R.string.room_hour);
    }
}
